package com.bby.screentool.service;

import static android.app.PendingIntent.FLAG_IMMUTABLE;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.widget.RemoteViews;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.bby.screentool.MainActivity;
import com.bby.screentool.R;

import org.webrtc.ScreenCapturerAndroid;


public class ScreenCaptureService extends Service {
    private static final String TAG = "===================>";
    private static final String mCustomChannelId="notification_record";
    private static final CharSequence mCustomChannelName="notification_record";
    private static final int mCustomNotificationId = 101;
    public static final String ACTION_STOP="ACTION_STOP_RECORD";
    private ScreenCapturerAndroid screenCapturerAndroid;

    public ScreenCapturerAndroid getScreenCapturerAndroid() {
        return screenCapturerAndroid;
    }

    public class CaptureScreenBinder extends Binder {
        public ScreenCaptureService getCaptureScreenService() {
            return ScreenCaptureService.this;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new CaptureScreenBinder();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent.getBooleanExtra("stop",false)){
            stopForeground(true);
            stopSelf();
            return START_STICKY;
        }
        createNotificationChannel();
        return super.onStartCommand(intent, flags, startId);
    }

    private void createNotificationChannel() {
        // 适配8.0及以上
        NotificationChannel channel = new NotificationChannel(mCustomChannelId, mCustomChannelName, NotificationManager.IMPORTANCE_DEFAULT);
        NotificationManager mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        mManager.createNotificationChannel(channel);

        // 适配12.0及以上
        int mFlag = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_IMMUTABLE : PendingIntent.FLAG_UPDATE_CURRENT;

        // 添加自定义通知view
        RemoteViews views = new RemoteViews(getPackageName(), R.layout.layout_notification);

        // 添加暂停继续事件
        Intent intentStop = new Intent(ACTION_STOP);
        PendingIntent pendingIntentStop = PendingIntent.getBroadcast(this, 0, intentStop, mFlag);
        views.setOnClickPendingIntent(R.id.bt_notification_stop, pendingIntentStop);

        Intent main = new Intent(this, MainActivity.class); //点击后跳转的界面，可以设置跳转数据
        main.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, mCustomChannelId)
                .setSmallIcon(R.drawable.ic_app)
                .setAutoCancel(false)
                .setContentIntent(PendingIntent.getActivity(this, 0, main, FLAG_IMMUTABLE))
                .setCustomContentView(views)
                .setCustomBigContentView(views);// 设置自定义通知view

        // 发起通知
//        mManager.notify(mCustomNotificationId, mBuilder.build());
        startForeground(mCustomNotificationId, mBuilder.build());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}